﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace Indie_Game_Contest
{
    public enum CollisionSide { None, Left, Right, Top, Bottom };
    public enum ObjectType { Player,Enemy,Tile,PowerUp,ObjectCount };
    abstract public class CBaseObject
    {
        //////////////////////////////////////////////////////////////////////////
        // Data Members
        protected Rectangle srcRect = Rectangle.Empty;
        protected Vector2 position = Vector2.Zero;
        protected Vector2 center = Vector2.Zero;
        protected Color colorMask = Color.White;
        protected float rotation = 0;
        protected float scale = 1;
        protected int width = 1;
        protected int height = 1;
        bool collision = false;
        private Texture2D image;  
        protected int textureID;
        protected ObjectType type;

        //////////////////////////////////////////////////////////////////////////
        // Methods
        public abstract void Render();
        public abstract void Update(float dt);

        public abstract void LoadContent(Rectangle sourceRect, Vector2 vPos, Color mask, Texture2D tImage, float fRotation, float fScale, int nWidth, int nHeight, int nID);

        public virtual CollisionSide CheckCollision(CBaseObject obj)
        {
            float fThisWidth = width * scale;
            float fThisHeight = height;// * scale;
            float fObjWidth = obj.width * obj.scale;
            float fObjHeight = obj.height * obj.scale;

            Rectangle collisionBox1 = new Rectangle((int)position.X - (int)(fThisWidth/2),
                (int)position.Y - (int)(fThisHeight/2), (int)(fThisWidth)-10, (int)fThisHeight);

            Rectangle collisionBox2 = new Rectangle((int)obj.position.X - (int)(fObjWidth/2),
                (int)obj.position.Y - (int)(fObjHeight/2), (int)fObjWidth-10, (int)fObjHeight);
            
            if (collisionBox1.Left >= collisionBox2.Right || collisionBox1.Right <= collisionBox2.Left ||
                collisionBox1.Top >= collisionBox2.Bottom || collisionBox1.Bottom <= collisionBox2.Top)
                return CollisionSide.None;

            Vector2 deltaVec = position - obj.position;
            deltaVec.Normalize();

            if (Math.Abs(deltaVec.X) > Math.Abs(deltaVec.Y) + .1f)
            {
                if (deltaVec.X > 0) return CollisionSide.Left;
                else return CollisionSide.Right;
            }
            else
            {
                if (deltaVec.Y > 0) return CollisionSide.Top;
                else return CollisionSide.Bottom;
            }
        }

        //////////////////////////////////////////////////////////////////////////
        // Properties
        public Indie_Game_Contest.ObjectType Type
        {
            get { return type; }
            set { type = value; }
        }
        public Vector2 Center
        {
            get { return center; }
            set { center = value; }
        }
        public bool Collision
        {
            get { return collision; }
            set { collision = value; }
        }
        public int Height
        {
            get { return height; }
            set { height = value; }
        }
        public int Width
        {
            get { return width; }
            set { width = value; }
        }
        public float Scale
        {
            get { return scale; }
            set { scale = value; }
        }
        public float Rotation
        {
            get { return rotation; }
            set { rotation = value; }
        }
        public Microsoft.Xna.Framework.Color ColorMask
        {
            get { return colorMask; }
            set { colorMask = value; }
        }
        public int TextureID
        {
            get { return textureID; }
            set { textureID = value; }
        }
        public Microsoft.Xna.Framework.Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        protected Rectangle SrcRect
        {
            get { return srcRect; }
            set { srcRect = value; }
        }

        protected Texture2D Image
        {
            get { return image; }
            set { image = value; }
        }


    }
}
